import { authToken } from '@/utils/auth'

const authModule = {
    state: {
        token: authToken.hasToken(),
        user: null,//登录信息
        permissions: [],//权限
    },
    mutations: {
        SET_TOKEN (state, data) {
            authToken.setToken(data)
            state.token = data
        },
        SET_USER (state, data) {
            // 储存token值，token值的名称user
            localStorage.setItem('user', JSON.stringify(data))
            state.user = data
        },
        RESET_AUTH_STATE (state) {
            authToken.removeToken()
            localStorage.removeItem('menus')
            localStorage.removeItem('acl')
            state.token = null
            state.user = null
        },
        SET_PERMISSIONS (state, data) {
            state.permissions = data
        }
    },
    actions: {
        setToken ({ commit }, data) {
            commit('SET_TOKEN', data)
        },
        // 用户信息
        setUser ({ commit }, data) {
            commit('SET_USER', data)
        },
        // 退出登录的
        resetAuthState ({ commit }) {
            commit('RESET_AUTH_STATE')
        },
        setPermissions ({ commit }, data) {
            commit('SET_PERMISSIONS', data)
        },
    }
}

export default authModule